'use strict'

let granter = require('../../../modules/lib/granter');
let authorize = granter.authorize;

// 不要求登录，无登录信息 -> true
function testAuthorizeNoRequireLogin(test) {
    let req = {
        session: {
        }
    };
    
    let result1 = authorize(req, {});
    let result2 = authorize(req, {
        login: false
    });

    test.ok(result1);
    test.ok(result2);

    test.done();
}

// 不要求登录，有登录信息 -> true
function testAuthorizeNoRequireLoginButLogin(test) {
    let req = {
        session: {
            login: {
                user: {
                    role: {
                        privileges: [{
                            name: 'get_crowdfunding'
                        }]
                    }
                }
            }
        }
    };

    let result1 = authorize(req, {});
    let result2 = authorize(req, {
        login: false
    });

    test.ok(result1);
    test.ok(result2);

    test.done();
}

// 只要求登录，无登录信息 -> false
function testAuthorizeRequireLoginButNoLogin(test) {
    let req = {
        session: {
        }
    };

    let result = authorize(req, {
        login: true
    });

    test.ok(!result);

    test.done();
}

// 只要求登录，有登录信息 -> true
function testAuthorizeRequireLoginAndLogin(test) {
    let req = {
        session: {
            login: {
                user: {
                    role: {
                        privileges: [{
                            name: 'get_crowdfunding'
                        }]
                    }
                }
            }
        }
    };

    let result = authorize(req, {
        login: true
    });

    test.ok(result);

    test.done();
}

// 要求某项权限，无登录信息 -> false
function testAuthorizeRequireOnePrivilegeNoLogin(test) {
    let req = {
        session: {
        }
    };

    let result = authorize(req, {
        login: true,
        privileges: [
            'create_crowdfunding'
        ]
    });

    test.ok(!result);

    test.done();
}

// 要求某项权限，无该权限 -> false
function testAuthorizeRequireOnePrivilegeNotMeet(test) {
    let req = {
        session: {
            login: {
                user: {
                    role: {
                        privileges: [{
                            name: 'get_crowdfunding'
                        }]
                    }
                }
            }
        }
    };

    let result = authorize(req, {
        login: true,
        privileges: [
            'create_crowdfunding'
        ]
    });

    test.ok(!result);

    test.done();
}

// 要求某项权限，有该权限 -> true
function testAuthorizeRequireOnePrivilegeMeet(test) {
    let req = {
        session: {
            login: {
                user: {
                    role: {
                        privileges: [{
                            name: 'get_crowdfunding'
                        }]
                    }
                }
            }
        }
    };

    let result = authorize(req, {
        login: true,
        privileges: [
            'get_crowdfunding'
        ]
    });

    test.ok(result);

    test.done();
}

// 要求多项权限，并不是完全满足 -> false
function testAuthorizeRequireManyPrivilegeNotMeet(test) {
    let req = {
        session: {
            login: {
                user: {
                    role: {
                        privileges: [{
                            name: 'get_crowdfunding'
                        }]
                    }
                }
            }
        }
    };

    let result = authorize(req, {
        login: true,
        privileges: [
            'get_crowdfunding',
            'create_crowdfunding'
        ]
    });

    test.ok(!result);

    test.done();
}

// 要求多项权限，完全满足 -> true
function testAuthorizeRequireManyPrivilegeMeet(test) {
    let req = {
        session: {
            login: {
                user: {
                    role: {
                        privileges: [{
                            name: 'get_crowdfunding'
                        }, {
                            name: 'create_crowdfunding'
                        }],
                    }
                }
            }
        }
    };

    let result = authorize(req, {
        login: true,
        privileges: [
            'get_crowdfunding',
            'create_crowdfunding'
        ]
    });

    test.ok(result);

    test.done();
}

module.exports = {
    testAuthorizeNoRequireLogin,
    testAuthorizeNoRequireLoginButLogin,
    testAuthorizeRequireLoginButNoLogin,
    testAuthorizeRequireLoginAndLogin,
    testAuthorizeRequireOnePrivilegeNoLogin,
    testAuthorizeRequireOnePrivilegeNotMeet,
    testAuthorizeRequireOnePrivilegeMeet,
    testAuthorizeRequireManyPrivilegeNotMeet,
    testAuthorizeRequireManyPrivilegeMeet,
};
